<?php
 include_once('../../src/classes/class.mssql.php');
    
    $comtec_id_responsible = $_GET['id'];

    $db = new MSSQL();
    
    $texto = "SELECT tag_id_responsible,
		     name,
		     lastname1,
		     lastname2,
		     rut,
		     position,
                     
                     DATEPART(day,company_income_date) as income_day,                     
                     DATEPART(month,company_income_date) as income_month,
                     DATEPART(yyyy,company_income_date) as income_year,
                   
                     DATEPART(day,birthdate) as birthdate_day,                     
                     DATEPART(month,birthdate) as birthdate_month,
                     DATEPART(yyyy,birthdate) as birthdate_year,                     
                        
		     notes,
		     cod_mainteiner
		     FROM responsible 
                     WHERE comtec_id_responsible=".$comtec_id_responsible;
    
    $result = $db->fetch_array($db->query($texto));  
   
    $tag_id_responsible=$result['tag_id_responsible'];
    $name = $result['name'];
    $lastname1 = $result['lastname1'];
    $lastname2 = $result['lastname2'];
    $position  = $result['position'];  
    
    $rut = $result['rut'];       
    $rut_cod = substr($rut,-1,1);
    $rut = substr($rut,0,strlen($rut)-1);
      
    $income_day = $result['income_day'];
    $income_month = $result['income_month'];
    $income_year = $result['income_year'];
       
    $birthdate_day = $result['birthdate_day'];
    $birthdate_month = $result['birthdate_month'];
    $birthdate_year = $result['birthdate_year'];
    
    $notes = $result['notes'];
    $cod_mainteiner = $result['cod_mainteiner'];
    
    function extra($x){
     if ($x <= 9){
      $x = "0".$x;   
     }   
     return $x;   
    }
    
?>

<fieldset class="curved">
 <legend>Editar</legend>
  <table style="margin: 10px auto; border-spacing: 10px;">
   <tr>
      <td width="200"><label>Tag Id.</label></td>
      <td><input type="text" size="40" maxlength="50" id="tag_id_responsible" value="<?=$tag_id_responsible;?>"/></td>
      <td width="200"><label>C&oacute;digo</label></td>
      <td><input type="text" size="40" maxlength="10" id="cod_mainteiner" value="<?=$cod_mainteiner;?>"/></td>
   </tr>  
   <tr>
    <td width="200"><label>Rut</label></td>
    <td><input type="text" id="rut" size="34" maxlength="9" value="<?=$rut;?>" />-<input type="text" id="rut_cod" size="1" maxlength="1" value="<?=$rut_cod;?>"/></td>
    <td width="200"><label>Nombre</label></td>
    <td><input type="text" id="name" size="40" maxlength="50" value="<?=$name;?>"/></td></div>
   </tr>
   <tr>
    <td width="200"><label>Apellido Paterno</label></td>
    <td><input type="text" id="lastname1" size="40" maxlength="50" value="<?=$lastname1;?>"/></td>
    <td width="200"><label>Apellido Materno</label></td>
    <td><input type="text" id="lastname2" size="40" maxlength="50" value="<?=$lastname2;?>"/></td>
   </tr>
   <tr>
    <td width="200"><label>Cargo</label></td>
    <td><input type="text" id="position" size="40" maxlength="150" value="<?=$position;?>"/></td>
    <td width="200"><label>Fecha de ingreso</label></td>
    <td>
      <?$optionsXML = simplexml_load_file("../../xml/days.xml"); 
      $options =  $optionsXML->xpath("//day/text()");
      echo '<select id="income_day">';
      while(list( $key , $value) = each($options)) {
       if ($income_day == $key) { echo '<option value='.extra($key).' selected >' . $value . '</option>';}   
       else {echo '<option value='.extra($key).'>' . $value . '</option>';}
      }
      echo '</select>';?>
      
      <?$optionsXML = simplexml_load_file("../../xml/months.xml"); 
      $options =  $optionsXML->xpath("//month/text()");
      echo '<select id="income_month">';
      while(list( $key , $value) = each($options)) {
       if ($income_month == $key) { echo '<option value='.extra($key).' selected >' . $value . '</option>';}   
       else {echo '<option value='.extra($key).'>' . $value . '</option>';}
      }
      echo '</select>';?>  
       
      <?$optionsXML = simplexml_load_file("../../xml/years.xml"); 
      $options =  $optionsXML->xpath("//year/text()");
      echo '<select id="income_year">';
      while(list( $key , $value) = each($options)) {
       if ($income_year == $value) { echo '<option value='.$value.' selected >' . $value . '</option>';}   
       else {echo '<option value='.$value.'>' . $value . '</option>';}
      }
      echo '</select>';?> 
        
    </td>
   </tr>
   <tr>  
    <td width="200"><label>Fecha de nacimiento</label></td>
    <td>
     
      <?$optionsXML = simplexml_load_file("../../xml/days.xml"); 
      $options =  $optionsXML->xpath("//day/text()");
      echo '<select id="birthdate_day">';
      while(list( $key , $value) = each($options)) {
       if ($birthdate_day == $key) { echo '<option value='.extra($key).' selected >' . $value . '</option>';}   
       else {echo '<option value='.extra($key).'>' . $value . '</option>';}
      }
      echo '</select>';?> 
        
      <?$optionsXML = simplexml_load_file("../../xml/months.xml"); 
      $options =  $optionsXML->xpath("//month/text()");
      echo '<select id="birthdate_month">';
      while(list( $key , $value) = each($options)) {
       if ($birthdate_month == $key) { echo '<option value='.extra($key).' selected >' . $value . '</option>';}   
       else {echo '<option value='.extra($key).'>' . $value . '</option>';}
      }
      echo '</select>';?> 
     
      <?$optionsXML = simplexml_load_file("../../xml/years.xml"); 
      $options =  $optionsXML->xpath("//year/text()");
      echo '<select id="birthdate_year">';
      while(list( $key , $value) = each($options)) {
       if ($birthdate_year == $value) { echo '<option value='.$value.' selected >' . $value . '</option>';}   
       else {echo '<option value='.$value.'>' . $value . '</option>';}
      }
      echo '</select>';?>
        
     </td>
    </tr>
   <tr>
    <td width="200"><label>Notas</label></td>
    </tr>
    <tr>
     <td colspan="4"><textarea id="notes" rows="3" cols="120" style="resize:none"><?=$notes;?></textarea></td>
    </tr>
    <tr>
     <td>
         <input type="button" id="send" value="Editar" onclick="javascript:editResponsible(<?=$comtec_id_responsible;?>)" />
         <input type="button" value="Cancelar" onclick="javascript:loadResponsibles();" />
     </td>
    </tr>
    </table>   
    <div id="result"></div> 
    </fieldset>

